package com.imay.live.view.custom;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.imay.live.R;
import com.imay.live.analytics.MobclickAgent;
import com.imay.live.proto.user.UserInfoCard;
import com.imay.live.utils.ViewUtil;

/**
 * Created by jeaman on 2017/3/24.
 * 直播UI
 */

public class RoomLiveShortView extends RoomBaseView implements RoomChallengeView.OnChallengeShowListener {
    private View mHideRootView; //右滑隐藏view
    private View mActionRightRootView;  //右滑不隐藏的view
    private View mCharmView;
    private View mWatermarkView;
    private RoomChallengeView mRoomChallengeView;
    private ImageView mLiveChallenge;
    private LinearLayout mLinearChallenge;
    private LinearLayout mlive_room_ll_user;
    private CountdownView mCountDown;
    private View mGiftView;
    private ImageView mLiveStatusIv;
    private OnLiveActionListener mListener;
    private boolean mSimpleMode = true;
    private boolean mHideAllView;   //未进入房间前，隐藏所有view
    private Runnable mHideChallengeRunnable;

    private int TIME = 1000 * 3;

    public RoomLiveShortView(Context context) {
        super(context);
    }

    public RoomLiveShortView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RoomLiveShortView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public RoomLiveShortView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    protected int getLayoutId() {
        return R.layout.view_room_live_short;
    }

    protected void init(Context context) {
        super.init(context);
        mTopRootView = findViewById(R.id.live_room_rl_top);
        mActionRootView = findViewById(R.id.live_room_fl_action);
        mHideRootView = findViewById(R.id.live_room_rl_hide);
        mActionRightRootView = findViewById(R.id.live_room_rl_action_right);
        mCharmView = findViewById(R.id.live_room_ll_charm);
        mWatermarkView = findViewById(R.id.live_room_ll_watermark);
        mLiveChallenge = ViewUtil.findView(this, R.id.iv_room_live_challenge);
        mLinearChallenge = ViewUtil.findView(this, R.id.ll_live_challenge);
        mLinearChallenge.setVisibility(INVISIBLE);
        mCountDown = (CountdownView) findViewById(R.id.live_room_cdv_push_countdown);
        mRoomChallengeView = ViewUtil.findView(this, R.id.live_room_rsv_chat);
        mGiftView = ViewUtil.findView(this, R.id.live_room_iv_gift);
        mlive_room_ll_user = (LinearLayout) findViewById(R.id.live_room_ll_user);
        mGiftView.setVisibility(GONE);//默认隐藏礼物按钮

        mRoomChallengeView.setOnChallengeShowListener(this);
        setChallengeContentVisibility(true);    //默认显示挑战内容
        initListener();
    }

    /**
     * 设置主播 直播状态
     */
    public void setLiveStatus(final UserInfoCard userInfoCard, final RoomLiveShortView.OnLiveActionListener actionListener) {
        if (userInfoCard == null) {
            return;
        }
        if (mLiveStatusIv == null) {
            mLiveStatusIv = new ImageView(getContext());
        }
        mLiveStatusIv.setVisibility(VISIBLE);
        mLiveStatusIv.setImageResource(userInfoCard.LiveStatus == 1 ? R.mipmap.btn_03_zhibozhong : R.mipmap.btn_03_cuizhibo);
        if (mlive_room_ll_user.indexOfChild(mLiveStatusIv) == -1) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
            mUserRecyclerView.setVisibility(GONE);
            params.leftMargin = (int) getResources().getDimension(R.dimen.dp_6);
            mlive_room_ll_user.addView(mLiveStatusIv, params);
        }
        mLiveStatusIv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (actionListener!=null)
                actionListener.onCallLiveClick(userInfoCard);
            }
        });
    }

    @Override
    protected void showInputView() {
        super.showInputView();
        setChallengeContentVisibility(false);//隐藏挑战题目
    }

    @Override
    public void setBottomViewVisibility(boolean visible) {
        setActionViewVisibility(visible);
        if (!mRoomChallengeView.isShowChallengeContent()) {
            setMessageViewVisibility(visible);
        }
        int visibility = visible ? VISIBLE : INVISIBLE;
        ViewUtil.startVisibilityAnimation(mRoomChallengeView, visibility);
    }

    public void startCountDown(long millisInFuture) {
        mCountDown.startCountDown(millisInFuture, null);
    }

    public void cancelCountDown() {
        mCountDown.cancelCountDown();
    }

    public void setCountDownTime(long millis) {
        mCountDown.setTime(millis);
    }

    // 设置挑战标题
    public void setChallengeTitle(String challengeTitle) {
        mRoomChallengeView.setChallengeTitle(challengeTitle);
    }

    // 设置挑战内容
    public void setChallengeContent(String challengeContent) {
        mRoomChallengeView.setChallengeContent(challengeContent);
    }

    // 隐藏/显示挑战内容
    public void setChallengeContentVisibility(boolean visible) {
        mRoomChallengeView.setChallengeContentVisibility(visible);
        setMessageViewVisibility(!visible); //同时隐藏或者显示公屏区
    }

    public void showChallengeNotify() {
        // 跟随弹泡
        if (mLinearChallenge == null) {
            return;
        }
        mHideChallengeRunnable = new Runnable() {
            @Override
            public void run() {
                mLinearChallenge.setVisibility(INVISIBLE);
            }
        };
        mLinearChallenge.setVisibility(VISIBLE);
        mLinearChallenge.postDelayed(mHideChallengeRunnable, TIME);
    }

    protected void initListener() {
//        super.initListener();
        findViewById(R.id.live_room_iv_gift).setOnClickListener(this);
        findViewById(R.id.live_room_iv_record).setOnClickListener(this);
        findViewById(R.id.live_room_iv_screenshot).setOnClickListener(this);
        findViewById(R.id.live_room_iv_share).setOnClickListener(this);
        findViewById(R.id.iv_room_live_challenge).setOnClickListener(this);
        findViewById(R.id.iv_room_live_girl).setOnClickListener(this);
    }

    //滑动隐藏view
    @Override
    public boolean toggleSimpleMode(boolean simple) {
        if (mSimpleMode == simple) {
            return false;
        }
        mSimpleMode = simple;
        int visibility = simple ? INVISIBLE : VISIBLE;
        ViewUtil.startVisibilityAnimation(mHideRootView, visibility);

        return true;
    }

    //隐藏除关闭按钮外的所有view
    public void hideAllView() {
        mHideAllView = true;
        toggleSimpleMode(true);
        ViewUtil.startVisibilityAnimation(mActionRightRootView, INVISIBLE);
    }

    public void showAllView() {
        mHideAllView = false;
        toggleSimpleMode(false);
        ViewUtil.startVisibilityAnimation(mActionRightRootView, VISIBLE);
    }

    @Override
    public void reset() {
        super.reset();
        mGiftView.setVisibility(GONE);
        mCountDown.setVisibility(GONE);
        setChallengeContentVisibility(true);    //默认显示挑战题目
        if (mLiveStatusIv != null) {
            mLiveStatusIv.setVisibility(GONE);
        }
    }

    @Override
    public boolean isHideAllView() {
        return mHideAllView;
    }

    public void setGiftViewVisibility(boolean visible) {
        mGiftView.setVisibility(visible ? VISIBLE : GONE);
    }

    public void setGirlLiveViewVisibility(boolean visible) {
        findViewById(R.id.iv_room_live_girl).setVisibility(visible ? VISIBLE : GONE);
    }

    @Override
    public void setActionViewVisibility(boolean visible) {
        super.setActionViewVisibility(visible);
        int visibility = visible ? VISIBLE : INVISIBLE;
        ViewUtil.startVisibilityAnimation(mActionRightRootView, visibility);
        ViewUtil.startVisibilityAnimation(mLiveChallenge, visibility);
    }

    public void setOnLiveActionListener(OnLiveActionListener pushActionListener) {
        super.setOnActionListener(pushActionListener);
        this.mListener = pushActionListener;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mLinearChallenge != null) {
            mLinearChallenge.removeCallbacks(mHideChallengeRunnable);
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.live_room_iv_gift:
                mListener.onGiftClick();
                break;
            case R.id.live_room_iv_record:
                mListener.onRecordClick();
                break;
            case R.id.live_room_iv_screenshot:
                mListener.onScreenShotClick();
                break;
            case R.id.live_room_iv_share:
                mListener.onShareClick();
                break;
            case R.id.iv_room_live_challenge:
                MobclickAgent.onEvent(getContext(), "cdb_watch_challenge");
                mListener.onChallengeClick();
            case R.id.iv_room_live_girl:
                mListener.onGirlLiveClick();
                break;
        }
    }

    @Override
    public void onChallengeShow(boolean show) {
        //挑战题目隐藏显示时显示公屏区
        setMessageViewVisibility(!show);
    }

    public interface OnLiveActionListener extends OnActionListener {
        void onGiftClick();

        void onScreenShotClick();

        void onShareClick();

        void onRecordClick();

        void onChallengeClick();

        void onGirlLiveClick();

        void onCallLiveClick(UserInfoCard userInfoCard);//召唤主播

    }
}
